package com.metro.demo.excal.utils;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class text {

    @Test
    public void test(){
        Long start = System.currentTimeMillis();
        createXml();
        System.out.println("运行时间："+ (System.currentTimeMillis() - start));
    }

    /**
     * 生成xml方法
     */
    public static void createXml(){
        try {
            // 创建解析器工厂
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            Document document = db.newDocument();
            // 不显示standalone="no"
            document.setXmlStandalone(true);
            Element workbook = document.createElement("workbook");

            // 向bookstore根节点中添加子节点book
            Element worksheet = document.createElement("worksheet");
            // 为worksheet节点添加属性
            worksheet.setAttribute("idx", "0");
            // 将worksheet节点添加到workbook根节点中
            workbook.appendChild(worksheet);

            Element section = document.createElement("section");
            // 不显示内容 name.setNodeValue("不好使");
            section.setAttribute("startRow","0");
            section.setAttribute("endRow","2");
            worksheet.appendChild(section);

            Element loop = document.createElement("loop");
            loop.setAttribute("startRow","3");
            loop.setAttribute("endRow","3");
            loop.setAttribute("items","biz4sinfos");
            loop.setAttribute("var","item");
            loop.setAttribute("varType","com.metro.demo.biz.beans.vo.Biz4sinfo");
            worksheet.appendChild(loop);

            Element section2 = document.createElement("section");
            section2.setAttribute("startRow","3");
            section2.setAttribute("endRow","3");
            loop.appendChild(section2);

            Element mapping = document.createElement("mapping");
            mapping.setAttribute("row","3");
            mapping.setAttribute("col","3");
            for (int i=0;i<10;i++){
                mapping.setTextContent("item.card");
            }
            section2.appendChild(mapping);

            // 将workbook节点（已包含worksheet）添加到dom树中
            document.appendChild(workbook);

            // 创建TransformerFactory对象
            TransformerFactory tff = TransformerFactory.newInstance();
            // 创建 Transformer对象
            Transformer tf = tff.newTransformer();

            // 输出内容是否使用换行
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            // 创建xml文件并写入内容
            tf.transform(new DOMSource(document), new StreamResult(new File("book1.xml")));
            System.out.println("生成book1.xml成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("生成book1.xml失败");
        }
    }
}
